package com.baidu.util;


import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.apache.commons.codec.binary.Base64.encodeBase64;
/**
 * @author 田甜
 * @version 1.0
 * @description: TODO
 * @date 2021/12/15 0015 9:25
 */
public class Identy {

    //身份证照片验证正不正确必要参数图片路径正面反面都会被识别

    //依赖请参照:https://icredit-code.oss-cn-hangzhou.aliyuncs.com/pom_v2.xml
        public static void main(String[] args, Map map) throws IOException {
            //API产品路径
            String requestUrl = "https://personcard.market.alicloudapi.com/ai-market/ocr/personid";
            //阿里云APPCODE
            String appcode = "5458f6797b984b4f9595b34f796b0349";

            CloseableHttpClient httpClient = null;
            try{
                httpClient = HttpClients.createDefault();
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // 装填参数
                if (false) {
                    //启用BASE64编码方式进行识别
                    //内容数据类型是BASE64编码
                    String imgFile = "图片路径";
                    String imgBase64 = "";
                    try {
                        File file = new File(imgFile);
                        byte[] content = new byte[(int) file.length()];
                        FileInputStream finputstream = new FileInputStream(file);
                        finputstream.read(content);
                        finputstream.close();
                        imgBase64 = new String(encodeBase64(content));
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                    params.add(new BasicNameValuePair("AI_IDCARD_IMAGE", imgBase64));
                    params.add(new BasicNameValuePair("AI_IDCARD_IMAGE_TYPE", "0"));
                    params.add(new BasicNameValuePair("AI_IDCARD_SIDE", "FRONT"));
                } else {
                    //启用URL方式进行识别
                    //内容数据类型是图像文件URL链接
                    params.add(new BasicNameValuePair("AI_IDCARD_IMAGE", "https://tse1-mm.cn.bing.net/th/id/R-C.f3d27b91da3abb09f1f5b627edda68e1?rik=nCvJ1cDhHOJiFg&riu=http%3a%2f%2fwww.ygjj.com%2fbookpic2%2f2013-12-19%2fnew507321-20131219212949655484.jpg&ehk=PSrAWvNlOtMpxm5MDtbPTSTgSfAibwQ8PSwwjuEZ%2b2A%3d&risl=&pid=ImgRaw&r=0"));
                    params.add(new BasicNameValuePair("AI_IDCARD_IMAGE_TYPE", "1"));
                    params.add(new BasicNameValuePair("AI_IDCARD_SIDE", "FRONT"));
                }
                // 创建一个HttpPost实例
                HttpPost httpPost = new HttpPost(requestUrl);
                httpPost.addHeader("Authorization","APPCODE " + appcode);
                httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                // 设置请求参数
                httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
                // 发送POST请求
                HttpResponse execute = httpClient.execute(httpPost);
                // 获取状态码
                int statusCode = execute.getStatusLine().getStatusCode();
                System.out.println(statusCode);
                // 获取结果
                HttpEntity entity = execute.getEntity();
                String result = EntityUtils.toString(entity, "utf-8");
                System.out.println(result);
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                if (httpClient != null) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
}
